package cn.xiyan.baas.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.xiyan.baas.entity.User;
import cn.xiyan.baas.service.IFileService;
import cn.xiyan.baas.service.IUserService;
import cn.xiyan.baas.utils.RedisClient;
import cn.xiyan.baas.vo.UserAddVo;
import cn.xiyan.baas.vo.UserQueryVo;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: ruiyanglee
 * @date: 2020-08-19 10:21
 * @description: user controller
 */

@Api(tags = "用户信息接口")
@RestController
@RequestMapping("/user")
@ApiSort(value = 1)
@CrossOrigin
public class UserController {
    public static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private RedisClient redisClient;

    @Resource
    private IUserService userService;

    @Resource
    private IFileService fileService;

    @GetMapping("/getUser")
    @ApiOperation(value = "根据用户id获取用户")
    // 获取参数
    @ApiImplicitParam(name = "id", value = "用户Id", required = true, dataType = "String")
    public Object getUser(@RequestParam
//            @ApiParam(required = true, name = "id", value = "用户id")
                                  String id) {
        User user = null;
        try {
            log.info("id {}", id);
            user = userService.findById(id);
            if (user == null) {
                log.error("user {}", "用户不存在");
                return "用户不存在";
            }
            return user;
        } catch (Exception e) {
            log.error(e.getMessage());
            return "新增用户失败";
        }
    }


    @GetMapping("/list")
    @ApiOperation(value = "获取用户列表")
    // 获取参数
    public Object list(HttpServletRequest request,HttpServletResponse response) {
//        response.setHeader("Access-Control-Allow-Origin",request.getHeader("origin"));
        List<User> list = userService.list();
        redisClient.set("/list", list);
        return list;
    }

    @PutMapping("")
    @ApiOperation(value = "分页获取用户列表")
    @ResponseBody
    public Object getUserByPage(@RequestBody UserQueryVo userQueryVo) {
        System.out.println(userQueryVo.getPage());
        Page<User> page = new Page<User>(userQueryVo.getPage(), userQueryVo.getSize());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        IPage<User> userIPage = userService.selectPage(page, null);
        return userIPage;
    }

    @ApiOperation(value = "新增用户")
    @PostMapping()
    public Object register(@RequestBody UserAddVo userVo) {
        try {
            User user = new User();
            BeanUtils.copyProperties(userVo, user);
            userService.save(user);
            return "添加成功";
        } catch (Exception e) {
            log.error(e.getMessage());
            return "新增失败";
        }
    }
}
